﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using control.Biz;

namespace TrangSucShop.usercontrol
{
    public partial class register : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string tenDN = txtUsername.Text;
            string matKhau = TextBox2.Text;
            string hoTen = TextBox3.Text;
            string email = TextBox4.Text;
            int sDT = 0;
            try { sDT = Convert.ToInt32(TextBox6.Text); }
            catch { }
            string diaChi = TextBox7.Text;
            DateTime begintime = new DateTime(2011, 11, 11);
            long ticktime = DateTime.Now.Ticks - begintime.Ticks;
            TimeSpan _timeSpan = new TimeSpan(ticktime);
            string maKH = "KH" + _timeSpan.TotalSeconds.ToString();
            BizPhanQuyen quyen = BizPhanQuyen.GetByID(0);
            BizKhachHang khachhang = new BizKhachHang()
            {
                ID = -1,
                Username = tenDN,
                Password = matKhau,
                TenKhachHang = hoTen,
                Email = email,
                SoDienThoai = sDT.ToString(),
                DiaChi = diaChi,
                MaKhachHang = maKH,
                Quyen = quyen,
                TaiKhoan=TextBox1.Text,
            };

            bool resuilt = khachhang.DangKy();
            if (resuilt == true)
                Response.Redirect("Default.aspx");
            else
                Label1.Text = "Tài khoản đã có người sử dụng";
        }

        protected void txtUsername_TextChanged(object sender, EventArgs e)
        {
            string _username = txtUsername.Text;
            BizKhachHang kh = BizKhachHang.FindByUsername(_username);

            if (kh != null)
            {
                UserAvailability.InnerText = "tên đăng nhập đã tồn tại.";
                UserAvailability.Attributes.Add("class", "taken"); ;
                Button1.Enabled = false;
            }
            else
            {
                if (_username != "")
                {
                    UserAvailability.InnerText = "tên đăng nhập hợp lệ";
                    UserAvailability.Attributes.Add("class", "available");
                    Button1.Enabled = true;
                }
                else
                {
                    UserAvailability.InnerText = "";
                    UserAvailability.Attributes.Add("class", "");
                    Button1.Enabled = true;
                }
            }
        }
    }
}